package com.xiaohudie.SGGJUC;

/**
 * @Author xiao hu die
 * @Date 2022/3/10 13:55
 * @Version 1.0
 */

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**阻塞队列*/
public class TestBlockQueue {
 public static void main(String[] args) throws InterruptedException {
 // test1();
 //  test2();
  //  test3();
 test4();
 }

 private static void test4() throws InterruptedException {
  ArrayBlockingQueue<Object> objects = new ArrayBlockingQueue<>(3);
  System.out.println(objects.offer(1, 2, TimeUnit.SECONDS));
  System.out.println(objects.offer(1, 2, TimeUnit.SECONDS));
  System.out.println(objects.offer(1, 2, TimeUnit.SECONDS));
  System.out.println(objects.offer(1, 2, TimeUnit.SECONDS));
  objects.poll(2,TimeUnit.SECONDS);
 }

 /**抛出异常*/
 private static void test1() {
  BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);
  System.out.println(blockingQueue.add(1));
  System.out.println(blockingQueue.add(2));
  System.out.println(blockingQueue.add(3));
 // System.out.println(blockingQueue.add(5));
  System.out.println(blockingQueue.remove());
  System.out.println(blockingQueue.remove());
  System.out.println(blockingQueue.remove());
  System.out.println(blockingQueue.remove());
 }
 /**有返回值，没有异常*/
 private static void test2() {
  ArrayBlockingQueue<Object> objects = new ArrayBlockingQueue<>(3);
  System.out.println(objects.offer(1));
  System.out.println(objects.offer(2));
  System.out.println(objects.offer(3));
  System.out.println(objects.offer(4));
  System.out.println(objects.poll());
  System.out.println(objects.peek());
  System.out.println(objects.poll());
  System.out.println(objects.poll());
  System.out.println(objects.poll());
 }
 /**阻塞*/
 private static void test3() throws InterruptedException {
  ArrayBlockingQueue<Object> objects = new ArrayBlockingQueue<>(3);
  objects.put(1);
  objects.put(1);
  objects.put(1);
 // objects.put(1);
  Object take = objects.take();
  System.out.println(take);
 }


}
